<template>
  <uploadExcel
    :on-success="onSuccess"
  />
</template>

<script>
import { importEmployee } from '@/api/employees'
export default {
  methods: {
    async onSuccess(data) {
      console.log(data.results)
      const newdata = data.results.map(item => {
        return this.chToEn(item)
      })
      console.log(newdata)
      await importEmployee(newdata)
      this.$message.success('导入成功')

      this.$router.back()
    },
    chToEn(zhUser) {
      const enUser = {}
      const zidian = {
        '入职日期': 'timeOfEntry',
        '手机号': 'mobile',
        '姓名': 'username',
        '转正日期': 'correctionTime',
        '工号': 'workNumber'
      }
      for (const key in zhUser) {
        const enkey = zidian[key]
        const value = zhUser[key]
        if (enkey === 'timeOfEntry' || enkey === 'correctionTime') {
          enUser[enkey] = new Date(this.formatExcelTime(value))
        } else {
          enUser[enkey] = value
        }
      }
      return enUser
    },
    formatExcelTime(num, format = '-') {
      num = Number(num)	// 强制类型转化，以防传来的值是字符串
      let millisecond = 0	// 转化后的毫秒数
      if (num > 60) {
        millisecond = (num - 25569) * 60 * 60 * 24 * 1000
      } else {
        millisecond = (num - 25568) * 60 * 60 * 24 * 1000
      }
      const date = new Date(millisecond)	// 根据转化后的毫秒数获取对应的时间
      const yy = date.getFullYear()
      const mm = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
      const dd = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
      return yy + format + mm + format + dd	// 返回格式化后的日期
    }
  }
}
</script>

<style>

</style>
